<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: 拔刀只为一人
  Date: 2023-06-25
  Time: 10:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script>
        function startTime(currentTime) {
            // 使用传递过来的时间进行初始化
            var current = new Date(currentTime);
            // 获取小时、分钟、秒钟
            var h = current.getHours();
            var m = current.getMinutes();
            var s = current.getSeconds();
            // 添加前导零
            m = checkTime(m);
            s = checkTime(s);
            // 更新时间显示
            document.getElementById('clock').innerHTML = h + ":" + m + ":" + s;
            // 每隔一秒钟调用一次自身，以更新时间显示
            setTimeout(function() {
                startTime(currentTime);
            }, 1000);
        }
        function checkTime(i) {
            if (i < 10) {i = "0" + i};
            return i;
        }
    </script>
</head>
<body onload="startTime('<%=getCurrentTime()%>')">

<div id="clock"></div>
</body>
<%!
    public String getCurrentTime() {
// 创建日期格式化器
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// 获取当前时间
        Date currentTime = new Date();
// 格式化当前时间并返回
        return sdf.format(currentTime);
    }
%>
</html>
